package com.example.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.example.app.retrofit2.api.ApiClient2;
import com.example.app.retrofit2.bean.LoginResultBean;
import com.example.app.retrofit2.bean.LoginSendBean;
import com.example.app.util.AuthUtil;
import com.example.app.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends Activity {
    private static final String TAG = "LoginActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText usernameY=findViewById(R.id.user_input);
        EditText passwordY=findViewById(R.id.password_input);
        Button login=findViewById(R.id.btn_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = usernameY.getText().toString();
                String password = passwordY.getText().toString();
                LoginSendBean loginSendBean = new LoginSendBean();
                loginSendBean.setUserName(username);
                loginSendBean.setPassword(password);
                Call<LoginResultBean> call = ApiClient2.getInstance(getApplicationContext()).
                        getReturnData(AuthUtil.GetAuth(),loginSendBean);
                call.enqueue(new Callback<LoginResultBean>() {
                    @Override
                    public void onResponse(Call<LoginResultBean> call, Response<LoginResultBean> response) {
                        if (response.body()!=null){
                            if (response.body()!=null){
                                if (response.body().getCode()==200){
                                    Log.d(TAG,response.body().getMsg().toString());
                                    Intent intent = new Intent(LoginActivity.this,ClockActivity.class);
                                    startActivity(intent);
//                                    ToastUtil.ShowToast(getApplicationContext(),"登录成功");
//                                    finish();
                                }
                            }
                        }else {
                            ToastUtil.ShowToast(getApplicationContext(),"网络连接错误");
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginResultBean> call, Throwable t) {
                        ToastUtil.ShowToast(getApplicationContext(),"网络请求错误");
                    }
                });

            }
        });
    }
}
